<?php
#############################################################################
#  Module Mini Webboard for Opencart 1.4.8 Up  From Team SiamOpencart.com		  											   #
#  เว็บผู้พัฒนา www.siamopencart.com ,www.thaiopencart.com                                                                                 #
#  โดย Somsak2004 วันที่ 23 พฤศจิกายน 2553                                                                                                           #
#############################################################################
# โดยการสนับสนุนจาก                                                                                                                                            #
# Unitedsme.com : ผู้ให้บริการเช่าพื้นที่เว็บไซต์ จดโดเมน ระบบ Linux                                                                              #
# Net-LifeStyle.com : ผู้ให้บริการเช่าพื้นที่เว็บไซต์์ จดโดเมน ระบบ Linux																           #
# SiamWebThai.com : SEO ขั้นเทพ โปรโมทเว็บขั้นเซียน ออกแบบ พัฒนาเว็บไซต์ / ตามความต้องการ และถูกใจ Google 		   #
#############################################################################
class ModelWebboardWebboard extends Model {
	
	public function getAllBoard() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_ask ");
		return	 $query->num_rows;
	}
	public function getAskTitle($id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_ask WHERE ask_id='".(int)$id."'");
		return	 $query->row['title'];
	}
	public function getAskBoard($id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_ask WHERE ask_id='".(int)$id."'");
		return	 $query->rows;
	}
	public function getQuessBoard($id,$start,$end) {
		$start =($start-1)*$end;
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_quess WHERE ask_id='".(int)$id."' LIMIT $start, $end ");
		return	 $query->rows;
	}
	public function getQuess($id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_quess WHERE quess_id='".(int)$id."' ");
		return	 $query->rows;
	}
	public function getQuessBoardAll($id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_quess WHERE ask_id='".(int)$id."'");
		return	 $query->num_rows;
	}
	public function getQuessBoardNumber($id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_quess WHERE ask_id='".(int)$id."'");
		return	 $query->num_rows;
	}
	public function getAskPost($start,$end) {
		$start =($start-1)*$end;
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "board_ask ORDER BY ask_id DESC LIMIT $start, $end ");
		return	 $query->rows;
	}
	public function InsertNewPost($title,$detail,$id_author) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "board_ask SET title = '" . $title . "', 
		detail = '" . $detail . "',   	date = NOW() , 
		author = '".$this->AuthorName($id_author)."', counter  ='0' ");
	}
	public function CheckDateOfQuess($id_ask) {
		$query=$this->db->query("SELECT date FROM " . DB_PREFIX . "board_quess  WHERE ask_id='".$id_ask."' ORDER BY quess_id DESC LIMIT 1 ");
		if (count($query->rows)==0) {
			return 999;
			}else{
			return floor((strtotime("now")	- strtotime($query->row['date']))/(3600*24));
			}
	}
	public function InsertNewPostByUser($title,$detail,$name) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "board_ask SET title = '" . $title . "', 
		detail = '" . $detail . "',   	date = NOW() , 
		author = '".$name."', counter  ='0' ");
	}
	public function InsertNewQuess($id,$detail,$id_author) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "board_quess SET ask_id = '" . $id . "', 
		reply_detail = '" . $detail . "',   	date = NOW() , 
		author = '".$this->AuthorName($id_author)."' ");
	}
	public function InsertNewQuessByUser($id,$detail,$name) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "board_quess SET ask_id = '" . $id . "', 
		reply_detail = '" . $detail . "',   	date = NOW() , 
		author = '".$name."' ");
	}
	public function AddRead($id) {
	$this->db->query("UPDATE  " . DB_PREFIX . "board_ask SET counter = counter+1 WHERE ask_id='".(int)$id."' ");

	}
	public function AuthorName($id) {
		if ($id != 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id='".(int)$id."' ");
		$query->row['firstname']= '<b><font color="#FF0000">'.$query->row['firstname'].'</b></font>' ;
		} else {
		$query->row['firstname']="Guest";
		}
	    return  $query->row['firstname'];
		
	}
	public function ShowUpdate($limit) {
	    $query=$this->db->query("SELECT * FROM ".DB_PREFIX."board_ask  ORDER BY ask_id DESC LIMIT $limit " );
        return $query->rows;
	}
	public function DeleteTopic($ask_id) {
	    $this->db->query("DELETE  FROM ".DB_PREFIX."board_ask  WHERE ask_id='".(int)$ask_id."'");
	    $this->db->query("DELETE  FROM ".DB_PREFIX."board_quess  WHERE ask_id='".(int)$ask_id."'");
	}
	public function DeleteQuess($quess_id) {
	    $this->db->query("DELETE  FROM ".DB_PREFIX."board_quess  WHERE quess_id='".(int)$quess_id."'");
	}
	public function UpdatePost($id,$title,$detail) {
		$this->db->query("UPDATE " . DB_PREFIX . "board_ask SET title = '" . $title . "', 
		detail = '" . $detail . "',   	date = NOW()
		WHERE ask_id='".(int)$id."' ");
	}
	public function UpdateQuess($id,$detail) {
		$this->db->query("UPDATE " . DB_PREFIX . "board_quess SET reply_detail = '" . $detail . "' ,  date = NOW()
		WHERE quess_id='".(int)$id."' ");
	}

}
?>